﻿using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using OfficeOpenXml;
using Payroll.BusinessLogic.CustomerManager;
using Payroll.Common.Utils;

namespace Payroll.BusinessLogic.ExportImport
{
    public partial class ExcelHelper
    {
        #region Methods
        /// <summary>
        /// Exports the send mail excel.
        /// </summary>
        /// <param name="resultCollection">The result collection.</param>
        /// <param name="filePath">The file path.</param>
        /// <param name="filetemplate">The filetemplate.</param>
        /// <exception cref="System.ArgumentNullException">filePath</exception>
        public static void ExportSendMailExcel(List<Results> resultCollection, ref string filePath, string filetemplate)
        {
            if (String.IsNullOrEmpty(filePath))
            {
                throw new ArgumentNullException("filePath");
            }

            FileInfo fileInfoTemplate = new FileInfo(filetemplate);
            FileInfo filewrite = fileInfoTemplate.CopyTo(filePath);
            using (var xlsx = new ExcelPackage(filewrite))
            {
                ExcelWorksheet sheetCust = xlsx.Workbook.Worksheets[1];
                sheetCust.Name = "DE NGHI CAP CKS - CNHCM";
                sheetCust.Cells[1, 3].Value = string.Format("Date Export {0}", DateTime.Now);
                sheetCust.Cells[2, 3].Value = string.Format("MẪU ĐỀ NGHỊ CẤP MỚI,GIA HẠN,CHUYỂN ĐỔI DỊCH VỤ CKS CA2");
                int startRow = 5;
                int resultCount = resultCollection.Count;
                if (resultCount > 2)
                {
                    sheetCust.InsertRow(startRow, resultCount, startRow);
                }
                foreach (var result in resultCollection)
                {
                    sheetCust.Cells[startRow, 1].Value = startRow - 4;
                    sheetCust.Cells[startRow, 2].Value = result.DatePurchase;
                    sheetCust.Cells[startRow, 3].Value = result.CustomerName;
                    sheetCust.Cells[startRow, 4].Value = result.TaxCode;
                    sheetCust.Cells[startRow, 5].Value = result.Address;
                    sheetCust.Cells[startRow, 6].Value = result.Mobile;
                    sheetCust.Cells[startRow, 7].Value = result.Email;
                    sheetCust.Cells[startRow, 8].Value = result.PackageCode;
                    sheetCust.Cells[startRow, 9].Value = result.PackageCode;
                    sheetCust.Cells[startRow, 10].Value = result.Name;
                    sheetCust.Cells[startRow, 11].Value = result.ServiceFree;
                    sheetCust.Cells[startRow, 12].Value = result.CustomerProfile;
                    startRow += 1;
                }
                xlsx.Save();
            }
        }
        #endregion
    }
}
